<?php
/* =========================================连接数据库================================================= */
include_once 'sql/get_information.php';
include_once 'sql/login_sql.php';

/* =========================================获取user的基本信息================================================= */
session_start();
$new_tweet_form = false;
$is_current_user = false;
if (!isset($_GET['user_id'])) {
    $user_id = $_SESSION['userid'];
    $new_tweet_form = true;
    $is_current_user = true;
}
else
    $user_id = $_GET['user_id'];

//获取用户最新的状态
$tweet_count = get_tweet_count($user_id);
if ($tweet_count <= 5) {
    $tweet = get_tweets($user_id, 1, $tweet_count);
} else {
    $tweet = get_tweets($user_id);
}

$currentUser = get_user($user_id);

/* =========================================获取user的兴趣爱好================================================= */
$tags = get_tags($user_id);

$userTag = "";
for ($i = 0; $i < count($tags); $i++) {
    $userTag .= $tags[$i]['user_tag'] . ";";
}

/* =========================================获取用户年龄================================================= */
$userAge = get_age($user_id);
if ($userAge > 1000) $userAge = "";
?>
<div data-role="page" data-theme="b" data-id="header">
    <div data-role="header" data-theme="b" data-id="header" data-position="fixed">
<!--        <a data-role="button" data-icon="" data-ajax="false" href="index.php?page=<?php //echo $_GET["back_page"];       ?>">返回</a>-->
        <a data-role="button" data-icon="back" data-rel="back">返回</a>

        <h2><?php echo $currentUser['nick_name'] . "的主页"; ?></h2>
        <!--通过地址栏传递参数  2011-7-7 首先判断是否关注该用户，如果没有则显示为关注按钮，如果有则显示为聊天按钮-->
        <?php
        if (!$is_current_user) {
            if (!is_following($_SESSION['userid'], $_GET['user_id'])) {
                //还没有关注
//                echo '<a id="follow_button" data-role="button" data-icon="gear" class="l-follow-button ui-btn-right" href="addFollowing(' . $_SESSION['userid'] . ',' . $_GET['user_id'] . ');void(0);">关注</a>';
                echo '<a id="follow_button" data-role="button" data-icon="gear" class="l-follow-button ui-btn-right" onmousedown="addFollowing(' . $_SESSION['userid'] . ',' . $_GET['user_id'] . ')">关注</a>';
                echo '<a id="chat_button" data-role="button" data-icon="gear" class="l-chat-button ui-btn-right l-displaynone" 
                   href="chat.php?currentuserid=' . $_SESSION['userid'] . '&userid=' . $_GET['user_id'] . '">聊天</a>';
            } else {
                //已经关注
//                echo '<a data-role="button" data-icon="gear" class="ui-btn-right" 
//                   href="add_following.php?userid=' . $_SESSION['userid'] . '&followerid=' . $_GET['user_id'] . '" class="l-displaynone">关注</a>';
                echo '<a data-role="button" data-icon="gear" class="ui-btn-right" 
                   href="chat.php?currentuserid=' . $_SESSION['userid'] . '&userid=' . $_GET['user_id'] . '">聊天</a>';
            }
        }
        ?>
    </div>
    <div data-role="content" class="l-inset-content">
        <div id="tweet">
            <ul id="tweet_list" class="l-inset l-detail l-profile-view " data-role="listview" data-inset="true" >
                <?php
                if ($new_tweet_form) {
                    ?>
                    <li data-role="list-divider" >最新动态：</li>
                    <li>
                        <form id="tweet_form">
                            <textarea id="new_tweet" class="ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" name="introduction" rows="8" cols="35">告诉朋友们你在做什么？</textarea>
                            <div>
                                <a id="submit_tweet" class=" l-button-right ui-btn ui-btn-up-c ui-btn-inline ui-btn-corner-all ui-shadow" data-inline="true" href="" data-role="button" data-theme="c">
                                    <span class="ui-btn-inner ui-btn-corner-all">
                                        <span class="ui-btn-text">发布</span>
                                    </span>
                                </a>
                            </div>
                        </form>
                    </li>

                    <?php
                    for ($i = 0; $i < count($tweet); $i++) {
                        $userTweetTime = $tweet[$i]['tweet_time'];
                        $userTweetContent = $tweet[$i]['tweet_content'];

                        //需要根据tweet_id来获取评论
                        $userTweetId = $tweet[$i]['tweet_id'];

                        echo ' <li class="l-tweet">
                        <div class="l-tweet-content">
                            <p><strong>' . $userTweetTime . '</strong></p>
                            <p>' . $userTweetContent . '</p>
                        </div></li>'
                        ?><?php
            }
                    ?>

                </ul>

                <!--bcuserid: 被评论用户id-->
                <?php
            }
            ?>

            </ul>
        </div>
        <div id="information">
            <ul id="information_list" class="l-inset l-detail l-profile-view " data-role="listview" data-inset="true">
                <li data-role="list-divider" >个人资料：</li>
                <li class="l-photo-contain">
                    <div class="l-photo l-shadowbox" style="background-image: url(<?php
            $URL = $currentUser['image'];
            echo ($URL == null ? './images/avatar.png' : small_to_large($URL));
            ?>)">    
                    </div>
                </li>
                <li><label>昵称：</label><label><?php echo $currentUser['nick_name']; ?></label></li>
                <li><label>位置：</label><label class="l-describe"><?php echo $currentUser['address']; ?></label></li>
                <li><label>爱好：</label><label class="l-describe"><?php echo $userTag; ?></label></li>
                <li><label>性别：</label><label><?php echo $currentUser['gender']; ?></label></li>
                <li><label>年龄：</label><label><?php echo $userAge; ?></label></li>
                <li><label>身高：</label><label><?php echo $currentUser['height'] == 0 ? "": $currentUser['height']. "cm"; ?></label></li>
                <li>
                    <label>简介：</label>
                    <label class="l-describe"><?php echo $currentUser['introduction']; ?></label>
                </li>
            </ul>
            <input type="hidden" name="userid" id="userid" value="<?php echo $currentUser['user_id']; ?>"/>
            <input type="hidden" name="nickname" id="nickname" value="<?php echo $currentUser['nick_name']; ?>"/>
        </div>
    </div>


    <div data-role="footer" data-theme="b" data-id="">
        <h2 >Team Luff</h2>
    </div>
    <script>
        function addFollowing(follower, followee){
            displayMessage("正在关注");
            $.get("add_following.php", {'userid':follower, 'followerid':followee}, function (result) {
                //                alert("followed");
                if(result == "success"){
                    hideMessage()
                    $(".l-chat-button").removeClass('l-displaynone');
                    $(".l-follow-button").addClass('l-displaynone');                    
                }

            })
        }
    </script>
</div>

